package com.example.scity.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.scity.APIInterface;
import com.example.scity.CitymetorActivity;
import com.example.scity.Configs;
import com.example.scity.ParkActivity;
import com.example.scity.R;
import com.example.scity.beens.GuangGaoBeen;
import com.example.scity.beens.ServiceListBeen;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Fragment1 extends Fragment {
    View view;
    Banner banner;
    RecyclerView recyclerView;
    ServiceListBeen serviceList;
    Fragment4 fragment4;
    FragmentManager fragmentManager;
    List<String> imglist = new ArrayList<>();
    static class ViewHodler1 extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;
        LinearLayout linearLayout;
        public ViewHodler1(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_img1);
            textView = itemView.findViewById(R.id.item_name1);
            linearLayout=itemView.findViewById(R.id.item_servilist_lin);
        }
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 1:
                    handlerbanner();
                    break;
                case 2:
                    handlerservicelist();
                    break;

            }
        }
    };

    private void handlerservicelist() {
        if(serviceList != null){
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
            recyclerView.setAdapter(new RecyclerView.Adapter() {
                @NonNull
                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_servilist,null);
                    return new ViewHodler1(view);
                }

                @Override
                public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                    ViewHodler1 viewHodler = (ViewHodler1) holder;
                    Glide.with(getActivity()).load(Configs.Host+serviceList.rows.get(position).imgUrl).into(viewHodler.imageView);
                    viewHodler.textView.setText(serviceList.rows.get(position).serviceName);
                    viewHodler.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            switch (viewHodler.getAdapterPosition()){
                                case 0:
                                    //停哪儿
                                    startActivity(new Intent(getActivity(), ParkActivity.class));
                                    break;
                                case 1:
                                    //城市地铁
                                    startActivity(new Intent(getActivity(), CitymetorActivity.class));
                                    break;
                            }
                        }
                    });
                }

                @Override
                public int getItemCount() {
                    return serviceList.rows.size();
                }
            });

        }
    }

    private void handlerbanner() {
        banner.setImages(imglist);
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context).load(o).into(imageView);
            }
        });
        banner.setDelayTime(2000);
        banner.start();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_1,container,false);
        fragment4 = new Fragment4();//初始化
        fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.ff,fragment4);//把fragment动态加载到ff中
        fragmentTransaction.commit();

        banner = view.findViewById(R.id.fragment1_banner);
        recyclerView = view.findViewById(R.id.fragment1_recl);
        loadbanner();

        loadServiceList();

        return view;
    }

    private void loadServiceList() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+APIInterface.ServiceList + "?isRecommend=N").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                serviceList = new Gson().fromJson(response.body().string(),ServiceListBeen.class);
                handler.sendEmptyMessage(2);
            }
        });
    }

    private void loadbanner() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host+ APIInterface.GuangGao+"?type=2").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                GuangGaoBeen json = new Gson().fromJson(response.body().string(), GuangGaoBeen.class);
                for (int i = 0;i<json.rows.size();i++){
                    imglist.add(Configs.Host+json.rows.get(i).advImg);
                }
                handler.sendEmptyMessage(1);
            }
        });
    }
}
